package com.lvmama.pangolin.util;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.entity.lmm.LMMTicketCommodity;
import com.lvmama.pangolin.entity.match.MatchTicketCommodity;
import com.lvmama.pangolin.entity.mt.MTTicketCommodityItem;
import com.lvmama.pangolin.entity.qnr.QNRTicketCommodityItem;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 商品关联度评分方法类
 * 该类主要用于手动进行匹配，鉴于匹配度的设置比较固定，在人为干预情况下
 * 尽量放宽匹配度，关联度比较高的商品排序都比较靠前，方便人工匹配
 * @Author:wangpeng
 * <h1>思路</h1>
 * <h2>将商品关联度评分分为多个维度（dimension）：</h2>
 * <p>维度1：商品名称</p>
 * <p>维度2：商品价格</p>
 * <p>维度3：商品类型</p>
 */
@Component
public class CommodityRelationshipUtil {

    private static Logger logger = Logger.getLogger(CommodityRelationshipUtil.class);

//    public final float PRICE_FACTOR=0.1F;//商品价格匹配度影响因子
//
//    public final float NAME_FACTOR=0.8F;//商品匹配度命名影响因子
//
//    public final float TYPE_FACTOR=0.1F;//商品匹配度商品分类影响因子

    @Autowired
    private CommodityMatchUtil commodityMatchUtil;

    /**
     * 获取价格影响度因子
     * @return
     */
    public float getPriceFactor()
    {
        String _PRICE_FACTOR = ConfigManager.getInstance().getParamValue("COMMODITY_FACTOR_PRICE","COMMODITY_FACTOR_PRICE");
        return (_PRICE_FACTOR!=null&&!"".equals(_PRICE_FACTOR))?Float.valueOf(_PRICE_FACTOR):0.1F;
    }

    /**
     * 获取名称影响度因子
     * @return
     */
    public float getNameFactor()
    {
        String _NAME_FACTOR = ConfigManager.getInstance().getParamValue("COMMODITY_FACTOR_NAME","COMMODITY_FACTOR_NAME");
        return (_NAME_FACTOR!=null&&!"".equals(_NAME_FACTOR))?Float.valueOf(_NAME_FACTOR):0.8F;
    }

    /**
     * 获取类型影响度因子
     * @return
     */
    public float getTypeFactor()
    {
        String _TYPE_FACTOR = ConfigManager.getInstance().getParamValue("COMMODITY_FACTOR_TYPE","COMMODITY_FACTOR_TYPE");
        return (_TYPE_FACTOR!=null&&!"".equals(_TYPE_FACTOR))?Float.valueOf(_TYPE_FACTOR):0.1F;
    }

    /**
     * 携程商品关联度评分，并按照关联度进行排序
     * @param commodity
     * @param list
     */
    public void matchAndSortXCTicketCommodity(LMMTicketCommodity commodity, List<XCTicketCommodityItem> list)
    {
        try{
            if(commodity == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = commodity.getCommodityName().trim();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(commodity.getCommodityName().trim());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(XCTicketCommodityItem item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getCommodity_name().trim());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float score = commodityMatchUtil.score(segName,msegName);//名称评分
                        float priceScore = commodityMatchUtil.priceScore(commodity.getSalePrice(),item.getSale_price());//价格因素评分
                        float typeScore = 0f;//票据类型评分
                        if(commodityMatchUtil.commodityType(name).equals(commodityMatchUtil.commodityType(item.getCommodity_name().trim())))
                        {
                            typeScore = 1f;
                        }
                        float scorem = priceScore*getPriceFactor() + score*getNameFactor() + typeScore*getTypeFactor();
//                        float scorem = priceScore*getPriceFactor() + score*getNameFactor() + typeScore*getTypeFactor();
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("携程商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<XCTicketCommodityItem>()
            {
                public int compare(XCTicketCommodityItem o1, XCTicketCommodityItem o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<XCTicketCommodityItem>() {
                @Override
                public int compare(XCTicketCommodityItem o1, XCTicketCommodityItem o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }

    }

    /**
     * 美团商品关联度评分，并按照关联度进行排序
     * @param commodity
     * @param list
     */
    public void matchAndSortMTTicketCommodity(LMMTicketCommodity commodity, List<MTTicketCommodityItem> list)
    {
        String preName = "";
        String segName = "";
        try{
            if(commodity == null || list == null || list.isEmpty())
            {
                return;
            }
            if(commodity.getCommodityName()==null || "".equals(commodity.getCommodityName().trim()))
            {
                commodity.setCommodityName(commodity.getSceneryName().trim());
            }
            String name = commodity.getCommodityName().trim();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(commodity.getCommodityName().trim());

            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(MTTicketCommodityItem item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getCommodity_name().trim());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }
                    if(preName!=null && segName != null)
                    {
                        if(preName.equals(mpreName)||segName.equals(msegName))
                        {
                            item.setDimension_all(1.0);//设置评分总数为1分
                        }else
                        {
                            float score = commodityMatchUtil.score(segName,msegName);//名称评分
                            float priceScore = commodityMatchUtil.priceScore(commodity.getSalePrice(),item.getSale_price());//价格因素评分
                            float typeScore = 0f;//票据类型评分
                            if(commodityMatchUtil.commodityType(name).equals(commodityMatchUtil.commodityType(item.getCommodity_name().trim())))
                            {
                                typeScore = 1f;
                            }
                            float scorem = priceScore*getPriceFactor() + score*getNameFactor() + typeScore*getTypeFactor();
                            item.setDimension_all(scorem);
                        }
                    }else{
                        item.setDimension_all(1.0);
                    }

                }
            }
        }catch (Exception e)
        {
            logger.error("美团商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<MTTicketCommodityItem>()
            {
                public int compare(MTTicketCommodityItem o1, MTTicketCommodityItem o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<MTTicketCommodityItem>() {
                @Override
                public int compare(MTTicketCommodityItem o1, MTTicketCommodityItem o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }
    }


    /**
     * 途牛商品关联度评分，并按照关联度进行排序
     * @param commodity
     * @param list
     */
    public void matchAndSortTNTicketCommodity(LMMTicketCommodity commodity, List<TNTicketCommodityItem> list)
    {
        try{
            if(commodity == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = commodity.getCommodityName().trim();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(commodity.getCommodityName().trim());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(TNTicketCommodityItem item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getCommodity_name().trim());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float score = commodityMatchUtil.score(segName,msegName);//名称评分
                        float priceScore = commodityMatchUtil.priceScore(commodity.getSalePrice(),item.getSale_price());//价格因素评分
                        float typeScore = 0f;//票据类型评分
                        if(commodityMatchUtil.commodityType(name).equals(commodityMatchUtil.commodityType(item.getCommodity_name().trim())))
                        {
                            typeScore = 1f;
                        }
                        float scorem = priceScore*getPriceFactor() + score*getNameFactor() + typeScore*getTypeFactor();
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("途牛商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<TNTicketCommodityItem>()
            {
                public int compare(TNTicketCommodityItem o1, TNTicketCommodityItem o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<TNTicketCommodityItem>() {
                @Override
                public int compare(TNTicketCommodityItem o1, TNTicketCommodityItem o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }
    }


    /**
     * 同城商品关联度评分，并按照关联度进行排序
     * @param commodity
     * @param list
     */
    public void matchAndSortTCTicketCommodity(LMMTicketCommodity commodity, List<TCommodityItem> list)
    {
        try{
            if(commodity == null || list == null || list.isEmpty())
            {
                return;
            }
            String name = commodity.getCommodityName().trim();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(commodity.getCommodityName().trim());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(TCommodityItem item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getCommodity_name().trim());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float score = commodityMatchUtil.score(segName,msegName);//名称评分
                        float priceScore = commodityMatchUtil.priceScore(commodity.getSalePrice(),item.getSale_price());//价格因素评分
                        float typeScore = 0f;//票据类型评分
                        if(commodityMatchUtil.commodityType(name).equals(commodityMatchUtil.commodityType(item.getCommodity_name().trim())))
                        {
                            typeScore = 1f;
                        }
                        float scorem = priceScore*getPriceFactor() + score*getNameFactor() + typeScore*getTypeFactor();
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("同城商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<TCommodityItem>()
            {
                public int compare(TCommodityItem o1, TCommodityItem o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<TCommodityItem>() {
                @Override
                public int compare(TCommodityItem o1, TCommodityItem o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }
    }


    /**
     * 去哪儿商品关联度评分，并按照关联度进行排序
     * @param commodity
     * @param list
     */
    public void matchAndSortQNRTicketCommodity(LMMTicketCommodity commodity, List<QNRTicketCommodityItem> list)
    {
        try{
            if(commodity == null || list == null || list.isEmpty())
            {
                return;
            }
            commodity.setCommodityName(commodity.getCommodityName().trim());
            String name = commodity.getCommodityName().trim();
            Map<String,String> lvmm = commodityMatchUtil.preHandelMatchTicketCommodity(commodity.getCommodityName().trim());
            String preName = "";
            String segName = "";
            if(lvmm!=null)
            {
                preName = lvmm.get("preName");
                segName = lvmm.get("segName");
            }
            if(list!=null && !list.isEmpty())
            {
                String mpreName = null;
                String msegName = null;
                for(QNRTicketCommodityItem item :list)
                {
                    if(item!=null)
                    {
                        Map<String,String> tmp = commodityMatchUtil.preHandelMatchTicketCommodity(item.getCommodity_name().trim());
                        mpreName=tmp.get("preName");
                        msegName=tmp.get("segName");
                    }

                    if(preName.equals(mpreName)||segName.equals(msegName))
                    {
                        item.setDimension_all(1.0);//设置评分总数为1分
                    }else
                    {
                        float score = commodityMatchUtil.score(segName,msegName);//名称评分
                        float priceScore = commodityMatchUtil.priceScore(commodity.getSalePrice(),item.getSale_price());//价格因素评分
                        float typeScore = 0f;//票据类型评分
                        if(commodityMatchUtil.commodityType(name).equals(commodityMatchUtil.commodityType(item.getCommodity_name().trim())))
                        {
                            typeScore = 1f;
                        }
                        float scorem = priceScore*getPriceFactor() + score*getNameFactor() + typeScore*getTypeFactor();
                        item.setDimension_all(scorem);
                    }
                }
            }
        }catch (Exception e)
        {
            logger.error("去哪儿商品按照关联度排序失败！"+e.getMessage(),e);

        }finally
        {
            //按照多维度评分进行排序
            Collections.sort(list, new Comparator<QNRTicketCommodityItem>()
            {
                public int compare(QNRTicketCommodityItem o1, QNRTicketCommodityItem o2)
                {
                    int temp = 0;
                    if(o1.getDimension_all() >o2.getDimension_all())
                    {
                        temp = -1;
                    }
                    else if(o1.getDimension_all() < o2.getDimension_all())
                    {
                        temp = 1;
                    }
                    return temp;
                }
            });
            //将已关联的数据放到最末尾
            Collections.sort(list,new Comparator<QNRTicketCommodityItem>() {
                @Override
                public int compare(QNRTicketCommodityItem o1, QNRTicketCommodityItem o2) {
                    if(o1.getIs_link()>o2.getIs_link())
                    {
                        return 1;
                    }else if(o1.getIs_link()==o2.getIs_link())
                    {
                        return 0;
                    }else
                    {
                        return -1;
                    }
                }
            });
        }
    }

}
